package com.xmut.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.xmut.mall.product.annotation.CheckJwt;
import com.xmut.mall.product.searchparam.ManagerProductSearchParam;
import com.xmut.mall.product.searchparam.ProductReviewSearchParam;
import com.xmut.mall.product.searchparam.SpuSearchParam;
import com.xmut.mall.product.utils.RespBean;
import com.xmut.mall.product.vo.AuthSpuVo;
import com.xmut.mall.product.vo.PageVO;
import com.xmut.mall.product.vo.ProductSpuSaveVo;
import com.xmut.mall.product.vo.SpuSaveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.product.entity.SpuInfoEntity;
import com.xmut.mall.product.service.SpuInfoService;
import com.xmut.mall.product.utils.PageUtils;
import com.xmut.mall.product.utils.R;


/**
 * spu信息
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 00:45:33
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 获取官方管理端的Spu分页数据
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getManagerSpuPage")
    public RespBean getManagerSpuPage(@RequestBody ManagerProductSearchParam param){
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return spuInfoService.getManagerSpuPage(vo,param);
    }

    /**
     * 商品下架
     * @param spuId
     * @return
     */
    @CheckJwt
    @PutMapping("/productOFF/{spuId}")
    public RespBean productOFF(@PathVariable Long spuId){
        return spuInfoService.productOFF(spuId);
    }

    /**
     * 商品spu上架
     * @param spuId
     * @return
     */
    @CheckJwt
    @PutMapping("/productUp/{spuId}")
    public RespBean productUp(@PathVariable Long spuId){
        return spuInfoService.productSpuUp(spuId);
    }


    /**
     * 获取spu对应的sku商品的库存信息
     * @param spuId
     * @return
     */
    @CheckJwt
    @GetMapping("/getSpuStock/{spuId}")
    public RespBean getSpuStock(@PathVariable Long spuId){
        return spuInfoService.getSpuStock(spuId);
    }

    /**
     * 商家端点击编辑获取Spu的全部信息
     * @param spuId
     * @return
     */
    @CheckJwt
    @GetMapping("/getSpuDetailWithEdit/{spuId}")
    public RespBean getSpuDetailWithEdit(@PathVariable Long spuId){
        return spuInfoService.getSpuDetailWithEdit(spuId);
    }

    /**
     * 获取spu的分页信息
     * @param param
     * @return
     */
    @CheckJwt
    @PostMapping("/getSpuByPage")
    public RespBean getSpuByPage(@RequestBody SpuSearchParam param){
        System.out.println("传入的参数:"+param);
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        return spuInfoService.getSpuByPage(vo,param);
    }

    /**
     * 获取spu的详情信息
     * @param id
     * @return
     */
    @CheckJwt
    @GetMapping("/getSpuDetail/{id}")
    public RespBean getSpuDetail(@PathVariable Long id){
        return spuInfoService.getSpuDetail(id);
    }

    /**
     * 审核Spu
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/authSpu")
    public RespBean authSpu(@RequestBody AuthSpuVo vo){
        return spuInfoService.authSpu(vo);
    }


    /**
     * 获取商品审核分页数据
     * @return
     */
    @CheckJwt
    @PostMapping("/getProductReviewByPage")
    public RespBean getProductReviewByPage(@RequestBody ProductReviewSearchParam param) {
        System.out.println("搜到的param:" + param);
        PageVO vo = new PageVO();
        BeanUtils.copyProperties(param,vo);
        System.out.println("属性拷贝后的PageVo:"+vo);

        return spuInfoService.getProductReviewByPage(vo,param);
    }

    /**
     * 保存商品Spu
     *
     * @param vo
     * @return
     */
    @CheckJwt
    @PostMapping("/saveSpu")
    public RespBean saveSpu(@RequestBody ProductSpuSaveVo vo) {
        System.out.println("拿到的VO:" + vo);
        return spuInfoService.saveSpu(vo);
    }

    /**
     * 根据spuId获取spu信息
     *
     * @param spuId
     * @return
     */
    @GetMapping("/getSpuInfoBySpuId/{spuId}")
    public RespBean getSpuInfoBySpuId(@PathVariable Long spuId) {
        SpuInfoEntity spuInfo = spuInfoService.getSpuInfoBySpuId(spuId);
        return RespBean.success("获取spuInfo成功", spuInfo);
    }

    /**
     * 上架功能
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId) {
        spuInfoService.productUp(spuId);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }


    /**
     * 修改
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuSaveVo vo) {
        spuInfoService.saveSpuInfo(vo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
